package com.gavindon.android.view.education

import android.os.Bundle
import android.view.*
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.leanback.widget.HorizontalGridView
import androidx.leanback.widget.VerticalGridView
import androidx.viewpager.widget.ViewPager
import com.gavindon.android.R
import com.gavindon.android.adapters.EducationAdapter
import com.gavindon.android.adapters.HorizontalTabAdapter
import com.gavindon.android.base.BaseFragment
import com.gavindon.android.base.INTENT_FRAGMENT_SMART
import com.gavindon.android.entity.SmartLeftTreeResp
import com.gavindon.android.network.DataResult
import com.gavindon.android.viewmodel.SmartEducationViewModel

/**
 * description:
 * Created by liNan on  2022-2-6 14:16
 */
class EducationFragment : BaseFragment() {
    override val layoutId = R.layout.fragment_education
    private lateinit var mViewModel: SmartEducationViewModel
    private lateinit var gridView: VerticalGridView
    private lateinit var horizontalTabsRv: HorizontalGridView
    private lateinit var mAdapter: EducationAdapter
    private lateinit var mTabAdapter: HorizontalTabAdapter
    private lateinit var emptyView: ConstraintLayout

    private val smartLeftTreeResp: SmartLeftTreeResp? by lazy {
        arguments?.getSerializable(INTENT_FRAGMENT_SMART) as SmartLeftTreeResp
    }
    private var tabLayoutIndex = -1

    override fun onInit(view: View, savedInstanceState: Bundle?) {
        mViewModel = getViewModel()
        tabLayoutIndex = arguments?.getInt("index", -1)!!
        gridView = view.findViewById(R.id.lean_grid_view)
        emptyView = view.findViewById(R.id.empty_view)
        horizontalTabsRv = view.findViewById(R.id.lean_horizontal_view)
        fetchData()
        gridView.apply {
            setItemSpacing(20)
            setHasFixedSize(true)
        }
        mAdapter = EducationAdapter()
        gridView.adapter = mAdapter
        mAdapter.setLeftOfUpFocusChanged {
            val act = this.activity as SmartEducationActivity
            when (it) {
                0 -> act.getLeftView().requestFocus()
                1 -> {
                    if (this::mTabAdapter.isInitialized) {
                        horizontalTabsRv.requestFocus()
                    } else {
                        act.getTopView().requestFocus()
                    }
                }
            }
        }


    }

    private fun fetchData() {
        smartLeftTreeResp?.let {
            when (it.showType) {
                0 -> gridView.setNumColumns(4) //微视频
                1 -> gridView.setNumColumns(4)
                2 -> gridView.setNumColumns(5)
                3 -> gridView.setNumColumns(2)
                else -> gridView.setNumColumns(4)
            }
            //如果三级children数据不为空的放需要动态添加tab
            if (!it.children.isNullOrEmpty()) {
                val act = this.activity as SmartEducationActivity
                //tabLayout position不准确
                initHorizontalTab(it.children)
                horizontalTabsRv.visibility = View.VISIBLE
                act.getViewPager().addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
                    override fun onPageScrolled(
                        position: Int,
                        positionOffset: Float,
                        positionOffsetPixels: Int
                    ) {
                    }

                    override fun onPageSelected(position: Int) {
                        if (position == tabLayoutIndex) {
                            horizontalTabsRv.requestFocus()
                        }
                    }

                    override fun onPageScrollStateChanged(state: Int) {
                    }

                })

            } else {
                horizontalTabsRv.visibility = View.GONE
                initData(it.subjectId)
            }
        }

    }

    /*三级目录tab*/
    private fun initHorizontalTab(children: List<SmartLeftTreeResp>) {
        mTabAdapter = HorizontalTabAdapter(R.layout.adapter_smart_three, children.toMutableList())
        horizontalTabsRv.adapter = mTabAdapter
        horizontalTabsRv.layoutManager?.scrollToPosition(0)
        mTabAdapter.setDpadChangedListener {
            val act = this.activity as SmartEducationActivity
            when (it) {
                0 -> {
                    act.getLeftView().requestFocus()
                }
                1 -> {
                    act.getTopView().requestFocus()
                }
                3 -> {
                    gridView.requestFocus()
                }
            }
        }
        mTabAdapter.setViewFocusChanged {
            initData(children[it].subjectId)
        }
    }

    private fun initData(subjectId: String) {
        mViewModel.getCourseLst(subjectId).observe(this, { resp ->
            if (resp is DataResult.Success) {
                mAdapter.setNewData(resp.data)
                gridView.visibility = View.VISIBLE
                emptyView.visibility = View.GONE
            } else if (resp is DataResult.Empty) {
                mAdapter.setNewData(null)
                gridView.visibility = View.GONE
                emptyView.visibility = View.VISIBLE
            }
        })

    }

}
